package com.ctl.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
//import sun.misc.BASE64Decoder;
//import sun.misc.BASE64Encoder;
import java.awt.*;
import java.util.Base64;
import java.util.Base64.Encoder;
import java.util.Base64.Decoder;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.MalformedURLException;
import java.util.Scanner;

public class ImageUtil {
    //	static BASE64Encoder encoder = new BASE64Encoder();
//	static BASE64Decoder decoder = new BASE64Decoder();
    Encoder encoder = Base64.getEncoder();
    Decoder decoder = Base64.getDecoder();
    static Logger logger = LoggerFactory.getLogger(ImageUtil.class);

    /**
     * 加载自定义字体
     * http://www.reeji.com/fonts/XIAODOU 可以下载免费字体
     * @param ttlfPath
     * @param style
     * @param size
     * @return
     */

    public static Font getFont(String ttlfPath, int style, float size) {
        InputStream is = null;
        try {
            is = new FileInputStream(ttlfPath);
            //返回一个指定字体类型和输入数据的font
            Font actionJson = Font.createFont(Font.TRUETYPE_FONT, is);
            //通过复制此 Font 对象并应用新样式和大小，创建一个新 Font 对象。
            Font actionJsonBase = actionJson.deriveFont(style, size);
            return actionJsonBase;
        } catch (Exception e) {
            logger.error("", e);
            return null;
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (Exception e) {

                }
            }
        }
    }

	/**
	 * 图片上加文字
	 * @param imagePath 图片路径
	 * @param newImagePath 新生成图片路径
	 * @param msg 内容
	 * @param x x坐标
	 * @param y y坐标
	 */

	public static void writeCharactersToImage(String imagePath,String newImagePath,String msg,int x,int y){
		try{
			// 获取图片
			BufferedImage image = ImageIO.read(new FileInputStream(imagePath));
			// 创建画笔
			Graphics2D pen = image.createGraphics();
			// 设置画笔颜色
			// pen.setColor(Color.WHITE);
			pen.setColor(new Color(28, 20, 10, 200));
			// 设置画笔字体样式为自定义字体，文字大小为50px
            		pen.setFont(new Font("微软雅黑", Font.ITALIC, 20));
			// 写文字和坐标
			pen.drawString(msg, x,y);
			// 创建新图片文件
			File file = new File(newImagePath);
			// 将处理好的图片数据写入到新图片文件中
			FileOutputStream fos = new FileOutputStream(file);
			ImageIO.write(image, imagePath.split("\\.")[1], fos);
		}catch (Exception e){
			logger.error("",e);
		}
	}
    public static void main(String[] args) throws FileNotFoundException, IOException {
//		snap install kcolorchooser
//		snap install gpick
        // 测试从图片文件转换为Base64编码
        //System.out.println(GetImageStr("d:\\wangyc.jpg"));
        //	base64StringToImage("iVBORw0KGgoAAAANSUhEUgAAAt8AAAEFCAMAAAAMmXsiAAAAAXNSR0IArs4c6QAAAKJQTFRF/////f//+/v/9fn/9/v/9fv/+f3/AAAAtdHZ/f3/9fn9/wAA/8eJ///H///n/6fn/+en/+f//8f//wBO/6ZO/06m/wCH/04ATqfl56dOisf+5///p+f/AIfHx4cJ/4jG/4cApFEAx///AAZnAE6ja06np05YhwAcgACH2Ker7efnh57eTgAU/05OoZShx8fn78fHTk5O572Hx//nx+enx//HG/XYjAAADIVJREFUeNrtmglf3EYSR/Xb658sM+MJAZvDw2HseMGOr+x+/6+2klpHd1W3EHhsxuP3koCQWlKReV2qLlH9HWB/qf4NsL/gN+A3wA/q9z8B9pfqHwD7C37DXvv9N4D9pfoXwP6C37DXfv8KsL/gN+A3AH4D4DfAd/QbAAAAAAB2ml8AtsGu+s0Uhz32CL8BvwHwG/CbuAC/iQvwG78Bj/Ab8BsAvwHwG/CbuAC/8RvwG78Bj/Ab8BsAvwHwG/CbuAC/8RvwG78Bj/Ab8BsAvwHwG/CbuAC/nzQudf/FP/vN4o6p4eN+O0qT4cQbqmbcoCdzbHFwcPCs2VgeHPz+W7ev+351cPA8OXTkB7sxfuOq21gfHHTnX433wu8d8Dsyx/qUiqMpdR/sd7h06mZyueGQYmKpk3MyARxdNiI+C18Wz2MtVy+etf+Nh46OK7PHj/EbR/XhdmN92N+034PfTx6XkuyXOqRIx4xhxtOS37Gn5oyM7m5kZtZZs8v5e/XisJO8cbe6Csk5+L247HXsDq3+bLPwcTTYjclsXF129xn8Hvbg9w74nftJJruWS4Ju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PHnF2szdPrl/32y9mHeLYdJzF7D/N3nDmrYjfc+125STHL7/iqiiuCR2dhbcVM2oP76HeatYZmn+tLKC1VZvmte/O3c1OPyaJp1F+bu/F7r/xWLFn+bWJOTSnq85kKOO1xK3mlM+23vcV3zd/4vX9+y7td8jtpoqi6N3+PmXV2/javgvAbv7fid9ImLNff5dWnmwlyD4cZfj/SMBM19Td+u8pV6U9xLy+pt5X1Qb5+zjRXXH/Q1DUPTdvud/g6N8nf+7q+BMBvwG/iAvzGb8Bv/AY8wm/AbwD8BsBvwG/iAvzGb8Bv/AY8wm/AbwD8BsBvwG/iAvzGb8Aj/AY8wm/AbwD8BvwmLsBv4gL8xm/AI/yGn95vgG3AFAcAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD4ZvwfznA+6CmfUdwAAAAASUVORK5CYII=");
//	  Scanner scanner=new Scanner(new File("D:\\1"));
//	  while(scanner.hasNext()){
//		  base64StringToImage(scanner.nextLine());
//	  }
//	  scanner.close();



        // 获取图片
        BufferedImage image = ImageIO.read(new FileInputStream("/home/gitee/commonUtils3/a.jpeg"));
        // 创建画笔
        Graphics2D pen = image.createGraphics();
        // 设置画笔颜色
        // pen.setColor(Color.WHITE);
        pen.setColor(new Color(28, 20, 10, 200));
        // 设置画笔字体样式为自定义字体，文字大小为50px
        String fontfilename = "/home/gitee/commonUtils3/f.ttf";
        pen.setFont(getFont(fontfilename, Font.ITALIC, 50));
        // 写文字和坐标
        pen.drawString("2015", 390, 1100);
        pen.drawString("06", 530, 1100);
        pen.drawString("03", 630, 1100);
        // 创建新图片文件
        File file = new File("/home/gitee/commonUtils3/b.jpeg");
        // 将处理好的图片数据写入到新图片文件中
        FileOutputStream fos = new FileOutputStream(file);
        ImageIO.write(image, "jpeg", fos);
        writeCharactersToImage("/home/gitee/commonUtils3/a.jpeg","/home/gitee/commonUtils3/b.jpeg","ctl",630,1300);


        String srcFile = "/home/gitee/commonUtils3/c.jpeg";
        String dstFile = "/home/gitee/commonUtils3/2.txt";
        int simpleSize = 3; //采样像素间隔 数值越大图像越小，精度越差
        buildAsciiImage(srcFile, dstFile, simpleSize);


    }
    // 一个字符对应一段灰度范围
    private static char[] charTable = {'$', '@', 'B', '%', '8', '&', 'W', 'M', '#', '*', 'o', 'a', 'h', 'k', 'b', 'd', 'p', 'q', 'w', 'm', 'Z', 'O', '0', 'Q', 'L', 'C', 'J', 'U', 'Y', 'X', 'z', 'c', 'v', 'u', 'n', 'x', 'r', 'j', 'f', 't', '/', '\\', '|', '(', ')', '1', '{', '}', '[', ']', '?', '-', '_', '+', '~', '<', '>', 'i', '!', 'l', 'I', ';', ':', ',', '"', '^', '`', '\'', '.', ' '};
//    private static char[] charTable = {'0'};
    /**
     * 创建ASCII字符画
     *
     * @param file
     * @param dstFile
     * @param simpleSize
     */
    public static void buildAsciiImage(String file, String dstFile, int simpleSize) {
        File srcFile = new File(file);

        BufferedImage bi = null;
        FileWriter fileWriter = null;
        try {
            bi = ImageIO.read(srcFile);
            if (bi == null) {
                return;
            }

            int step = simpleSize; //大小 数值越大图像越小
            fileWriter = new FileWriter(dstFile);
            StringBuilder strBuilder = new StringBuilder();
            for (int j = 0; j < bi.getHeight(); ) { //读取一行
                for (int i = 0; i < bi.getWidth(); ) { //读取一列
                    int color = bi.getRGB(i, j); //某一像素
                    char c = colorToChar(color);
                    strBuilder.append(c);
                    strBuilder.append(c);//默认字体是半角，输出的宽比较小，所以输出两遍
                    i += step;
                }
                strBuilder.append("\n");
                j += step;
            }
            String ascii = strBuilder.toString();
            System.out.print(ascii);
            fileWriter.write(ascii);
            fileWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileWriter != null) {
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 根据彩色点灰度获取对应字符
     * @param color
     * @return
     */
    public static char colorToChar(int color) {
        Color tcolor = new Color(color);
        if (tcolor.getAlpha() == 0) {
            return ' ';
        }
        /** 彩色转灰度，心理学公式，参数可调
         * 彩色转灰度算法参见 http://www.cnblogs.com/diewcs/archive/2010/10/03/1841744.html
         */
        //int gray = (2126 * tcolor.getRed() + 7152 * tcolor.getGreen() + 722 * tcolor.getBlue()) / 10000;
        int gray = (299 * tcolor.getRed() + 587 * tcolor.getGreen() + 114 * tcolor.getBlue() + 500) / 1000;
        int inext = gray * charTable.length / 256;
        return charTable[inext];
    }

    public static void base64StringToImage(String imageBase64Str) {
        base64StringToImage(imageBase64Str, "jpg", "D:\\out.jpg");
    }

    /**
     * @param imageBase64Str base64 字符串
     * @param suffix         图片格式  jgp  png
     * @param fileCreatePath 图片生成路径
     */
    public static void base64StringToImage(String imageBase64Str, String suffix, String fileCreatePath) {
        try {
            BufferedImage bufImg = ImageIO.read(new ByteArrayInputStream(Base64.getDecoder().decode(imageBase64Str)));
            if (null == bufImg) {
                return;
            } else {
                ImageIO.write(bufImg, suffix, new File(fileCreatePath));
                return;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String imageToBase64String(String filePath) throws FileNotFoundException, IOException {
        InputStream input = new BufferedInputStream(new FileInputStream(new File(filePath)));
        byte bytes[] = new byte[input.available()];
        input.read(bytes);
        input.close();
        return new String(Base64.getEncoder().encode(bytes));
    }

    public static String encodeImgageToBase64(File imageFile) {// 将图片文件转化为字节数组字符串，并对其进行Base64编码处理
        ByteArrayOutputStream outputStream = null;
        try {
            BufferedImage bufferedImage = ImageIO.read(imageFile);
            outputStream = new ByteArrayOutputStream();
            ImageIO.write(bufferedImage, "jpg", outputStream);
        } catch (MalformedURLException e1) {
            logger.error("error", e1);
        } catch (IOException e) {
            logger.error("error", e);
        }
        // 对字节数组Base64编码
        Encoder encoder = Base64.getEncoder();
        return encoder.encodeToString(outputStream.toByteArray());// 返回Base64编码过的字节数组字符串
    }


}